package com.sencha.myblog;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

/**
 * Servlet implementation class ListTopics
 */
public class ListTopics extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public ListTopics() {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("rawtypes")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("node");
		String username = (String) request.getSession().getAttribute("username");

		if (param.equals("root")) {

			List<Preference> list = null;
			if (username != null) {
				list = DaoManager.getInstance().getPreferences(username);
			} else {
				list = DaoManager.getInstance().getPreferences("user1");
			}

			JSONObject data = new JSONObject();
			JSONArray items = new JSONArray();
			try {
				data.put("text", "我的订阅博客");
				data.put("items", items);
				for (Preference pref : list) {
					JSONObject prefObject = new JSONObject();
					items.add(prefObject);
					prefObject.put("text", pref.getTitle());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			response.setContentType("application/json; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println(data.toString());
		} else {
			String title = new String(param.getBytes("iso-8859-1"), "utf-8");
			if (title != null) {
				Preference pref = DaoManager.getInstance().getPreference(title);
				String result = HttpClient.doGet(pref.getUrl());
				result = new String(result.getBytes(), "utf-8");
				Document doc = null;
				JSONObject data = new JSONObject();
				JSONArray items = new JSONArray();
				try {
					doc = DocumentHelper.parseText(result);
					List nodes = doc.selectNodes("rss//item/title");
					List nodes_link = doc.selectNodes("rss//item/link");
					List nodes_desc = doc.selectNodes("rss//item/description");
					data.put("text", title);
					data.put("items", items);
					for (int i = 0; i < nodes.size(); i++) {
						Node node = (Node) nodes.get(i);
						Node node_link = (Node) nodes_link.get(i);
						Node node_desc = (Node) nodes_desc.get(i);
						if (node instanceof Element) {
							JSONObject object = new JSONObject();
							object.put("text", ((Element) node).getText());
							object.put("link", ((Element) node_link).getText());
							object.put("description", ((Element) node_desc).getText());
							object.put("leaf", "true");
							items.add(object);
						}

					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				response.setContentType("application/json; charset=utf-8");
				PrintWriter out = response.getWriter();
				out.println(data.toString());
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
